/**
 * 
 */
package com.csair.youth.web.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.csair.youth.constant.Constant;
import com.csair.youth.database.Role;
import com.csair.youth.database.User;
import com.csair.youth.domain.query.QueryParam;
import com.csair.youth.domain.query.UserQueryBean;
import com.csair.youth.service.impl.RoleServiceImpl;
import com.csair.youth.service.impl.UserServiceImpl;

/**
 * @author mickey
 *
 */
public class UserEditAction extends _BaseAction {
	private User item;
	private UserServiceImpl service;
	private List roles;
	private RoleServiceImpl roleService;

	public User getItem() {
		return item;
	}

	public void setItem(User item) {
		this.item = item;
	}

	public UserServiceImpl getService() {
		return service;
	}

	public void setService(UserServiceImpl service) {
		this.service = service;
	}

	public List getRoles() {
		return roles;
	}

	public void setRoles(List roles) {
		this.roles = roles;
	}

	public RoleServiceImpl getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleServiceImpl roleService) {
		this.roleService = roleService;
	}

	public String execute(){
		roles = roleService.getRoles();
		if(item != null && !StringUtils.isEmpty(item.getId())){
			//从数据库获取...
			item = service.getUser(item.getId());
		}
		else item = new User();
		return SUCCESS;
	}
	
	public String save(){
		if(item == null) {
			this.setMessage("用户对象为空.");
			return INPUT;
		}
		try {
			if(!validInput(item)) return INPUT;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace(); return INPUT;
		}
		if(!service.isLoginNameUnique(item.getLoginName(), item.getId())){
			this.setMessage("用户登录名已存在.");
			return INPUT;
		}
		service.saveUser(item);
		this.setMessage("已成功保存用户.");
		return SUCCESS;
	}
	
	public String goPasswordPage(){
		return SUCCESS;
	}
	
	public String changePassword(){
		String userId = (String)getSessionAttribute(Constant.USER_ID_SESSION_KEY);
		if(userId == null){
			this.setMessage("用户不存在!");
			return INPUT;
		}
		item = service.getUser(userId);
		if(item != null){
			if(!validPassword(item)){
				return INPUT;
			}
			else{
				service.updateUserPassword(item, service.generatePassword(this.getServletRequest().getParameter("newpw"), true));
				this.setMessage("密码修改成功.");
				return SUCCESS;
			}
		}
		else{
			this.setMessage("用户不存在!");
			return INPUT;
		}
	}
	
	private boolean validPassword(User item){
		HttpServletRequest req = this.getServletRequest();
		String oldpw = req.getParameter("oldpw");
		String newpw = req.getParameter("newpw");
		String renewpw = req.getParameter("renewpw");
		try {
			if(!service.isPasswordRight(oldpw, item.getPassword())){
				this.setMessage("旧密码错误.");
				return false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.setMessage("验证旧密码时发生错误.");
			return false;
		}
		if(!StringUtils.equals(newpw, renewpw)){
			this.setMessage("两次输入新密码不匹配.");
			return false;
		}
		return true;
	}
	
	private boolean validInput(User item) throws UnsupportedEncodingException{
		if(item == null) return false;
		boolean allValid = true;
		StringBuffer msgBf = new StringBuffer();
		String name = item.getName();
		if(StringUtils.isEmpty(name) || StringUtils.length(name)> 10){
			msgBf.append("用户名不能为空且必须在10个字符内<br/>");
			allValid = false;
		}
		String loginName = item.getLoginName();
		if(StringUtils.isEmpty(loginName) || loginName.getBytes("gbk").length > 20){
			msgBf.append("登录名不能为空且必须在20个字节内<br/>");
			allValid = false;
		}
		String email = item.getEmail();
		if(!StringUtils.isEmpty(email) && email.getBytes("gbk").length > 50){
			msgBf.append("email必须在50个字节内<br/>");
			allValid = false;
		}
		String enableIp = item.getEnableIp();
		if(!StringUtils.isEmpty(enableIp) && enableIp.getBytes("gbk").length > 255){
			msgBf.append("允许IP必须在255个字节内<br/>");
			allValid = false;
		}
		String remark = item.getRemark();
		if(!StringUtils.isEmpty(remark) && StringUtils.length(remark) > 120){
			msgBf.append("备注必须在120个字符内<br/>");
			allValid = false;
		}
		this.setMessage(msgBf.toString());
		return allValid;
	}
	
	public void prepare() throws Exception {
		roles = new ArrayList();
		Role all = new Role();
		all.setId("");
		all.setName("所有");
		roles.add(all);
		roles.addAll(roleService.getRoles());
	}
}
